<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="500"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="100px"
      label-position="right"
      class="noFlex"
    >
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="form.roleName" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色别名" prop="antherName">
        <el-input v-model="form.antherName" clearable></el-input>
      </el-form-item>
      <!-- <el-form-item label="数据权限" prop="dataAuthType"> -->
      <!-- <el-input-number
          v-model="form.dataAuthType"
          controls-position="right"
          :min="1"
          style="width: 100%"
        ></el-input-number> -->
      <!-- <el-select
          v-model="form.dataAuthType"
          placeholder="请选择"
          style="width: 100%"
          clearable
        >
          <el-option
            v-for="item in dataAuthTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select> -->
      <!-- </el-form-item> -->
      <el-form-item label="角色编码" prop="roleCode">
        <el-input v-model="form.roleCode" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="enable">
        <el-switch
          v-model="form.enable"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="mode == 'show'">
        <el-input v-model="form.createTime" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色说明" prop="remark">
        <el-input v-model="form.remark" clearable type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ['success', 'closed'],
  data () {
    return {
      mode: "add",
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        id: "",
        roleName: "",//角色名称
        antherName: "",//角色别名
        // dataAuthType: '',//数据权限  
        roleCode: '',//角色编码
        enable: 1,//状态  1启用0禁用
        createTime: '', //创建时间
        remark: ""//角色说明
      },
      dataAuthTypeOptions: [
        {
          value: 0,
          label: '全部可见',
        },
        {
          value: 1,
          label: '本人可见',
        },
        {
          value: 2,
          label: '本部门可见',
        },
        // {
        //   value: 3,
        //   label: '本部门及下级可见',
        // },
        // {
        //   value: 4,
        //   label: '选择部门可见',
        // },
        // {
        //   value: 5,
        //   label: '自定义可见',
        // },
      ],
      //验证规则
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称' }
        ],
        antherName: [
          { required: true, message: '请输入角色别名' }
        ],
        dataAuthType: [
          { required: true, message: '请选择数据权限类型', trigger: 'change' }
        ]
      }
    }
  },
  mounted () {

  },
  methods: {
    //显示
    open (mode = 'add') {
      this.mode = mode
      this.visible = true
      return this
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let data = {
            roleName: this.form.roleName,
            antherName: this.form.antherName,
            dataAuthType: this.form.dataAuthType,
            roleCode: this.form.roleCode,
            enable: this.form.enable,
            remark: this.form.remark,
          }
          let res = null
          if (this.mode == 'add') {
            res = await this.$API.role.addRole.post(data)
          } else if (this.mode == 'edit') {
            data.id = this.form.id
            res = await this.$API.role.updateRole.put(data)
          }
          console.log(res)
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.visible = false
            this.$message.success("操作成功")
          }
        }
      })
    },
    //表单注入数据
    setData (data) {
      this.form.id = data.id
      this.form.roleName = data.roleName
      this.form.antherName = data.antherName
      // this.form.dataAuthType = data.dataAuthType
      this.form.roleCode = data.roleCode
      this.form.enable = data.enable
      this.form.createTime = data.createTime
      this.form.remark = data.remark

      //可以和上面一样单个注入，也可以像下面一样直接合并进去
      //Object.assign(this.form, data)
    }
  }
}
</script>

<style>
</style>
